﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ThumbsRemove
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            txtWebFolder.Text = Properties.Settings.Default.LastPath;
        }

        private void btnFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                txtWebFolder.Text = fbd.SelectedPath;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            string imagesPath = Path.Combine(txtWebFolder.Text, "images");
            string thumbsPath = Path.Combine(txtWebFolder.Text, "thumbnails");

            if (!Directory.Exists(imagesPath))
            {
                MessageBox.Show(string.Concat("Non trovo ", imagesPath));
                return;
            }

            if (!Directory.Exists(thumbsPath))
            {
                MessageBox.Show(string.Concat("Non trovo ", thumbsPath));
                return;
            }

            Properties.Settings.Default.LastPath = txtWebFolder.Text;
            Properties.Settings.Default.Save();

            foreach (string file in Directory.GetFiles(thumbsPath, "*.jpg"))
            {
                string imagefile = Path.Combine(imagesPath, Path.GetFileName(file));
                if (!File.Exists(imagefile))
                {
                    ListViewItem lvi = listView1.Items.Add("Delete", "colDelete");
                    lvi.Name = "colAction";
                    lvi.SubItems.Add(Path.GetFileName(file)).Name = "colFile";
                    File.Delete(file);
                }
            }

            foreach (string ifile in Directory.GetFiles(imagesPath, "*.jpg"))
            {
                string thumbfile = Path.Combine(thumbsPath, Path.GetFileName(ifile));
                if (!File.Exists(thumbfile))
                {
                    ListViewItem lvi = listView1.Items.Add("Missing", "colDelete");
                    lvi.Name = "colAction";
                    lvi.SubItems.Add(Path.GetFileName(ifile)).Name = "colFile";
                }
            }
        }
    }
}
